package com.shujia.flink.core

import org.apache.flink.streaming.api.scala._

object Demo2StreamWordCount {
  def main(args: Array[String]): Unit = {


    /**
      *
      * 创建flink环境
      * StreamExecutionEnvironment  流处理所需要的环境
      *
      *
      */

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    //读取socket数据
    // nc -lk 9999
    val linesDS: DataStream[String] = env.socketTextStream("master", 9999)

    //实时统计单词数量   累加统计
    val countDS: DataStream[(String, Int)] = linesDS
      .flatMap(_.split(","))
      .map(word => (word, 1))
      .keyBy(kv => kv._1)
      .sum(1)


    //内部调用sink
    countDS.print()


    //启动flink程序
    env.execute()


  }

}
